package com.guoby.controller.user;

import com.guoby.model.Result;
import com.guoby.model.User;
import com.guoby.repository.UserRepository;
import com.guoby.service.UserService;
import com.guoby.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by pactera on 2018/3/9.
 */
@RestController
public class UserController {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserService userService;

    /***
     * 查询所有用户信息
     * @return
     */
    @GetMapping("/users")
    public List<User> getUserlist() {
        return userRepository.findAll();
    }

    /***
     * 添加用户
     * @return
     * @RequestParam("username") String name,
     @RequestParam("password") String pass, @RequestParam("age") Integer age
     */
    @PostMapping("/users")
    public Result<User> addUser(@Valid User u, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
            return ResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
        }
        User user = new User();
        user.setUsername(u.getUsername());
        user.setPassword(u.getPassword());
        user.setAge(u.getAge());

        return ResultUtil.success(userRepository.save(user)) ;
    }

    /***
     * 根据Id查询
     * @param id
     * @return
     */
    @GetMapping("/users/{id}")
    public User getUserById(@PathVariable("id") Integer id) {
        return userRepository.findOne(id);
    }

    /***
     * 根据Id更新
     * @param id
     * @param name
     * @param pass
     * @param age
     * @return
     */
    @PutMapping("/users/{id}")
    public User updateUserById(@PathVariable("id") Integer id,
                               @RequestParam("username") String name,
                               @RequestParam("password") String pass,
                               @RequestParam("age") Integer age) {
        User user = new User();
        user.setUsername(name);
        user.setPassword(pass);
        user.setAge(age);
        user.setId(id);
        return userRepository.save(user);
    }
    @PutMapping("/users2/{id}")
    public User updateUserById(@PathVariable("id") Integer id, User u) {
        User user = new User();
        user.setUsername(u.getUsername());
        user.setPassword(u.getPassword());
        user.setAge(u.getAge());
        user.setId(id);
        return userRepository.save(user);
    }

    /***
     * 根据Id删除
     * @param id
     */
    @DeleteMapping("/users/{id}")
    public void deleteUserById(@PathVariable("id") Integer id) {
        userRepository.delete(id);
    }

    @GetMapping("/users/age/{age}")
    public List<User> userListByAge(@PathVariable("age") Integer age) {
        return userRepository.findByAge(age);
    }

    @PostMapping("/users/two")
    public void userTwo() {
        userService.inserTwo();
    }
}
